package first;

import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
import java.util.Scanner;

public class Client extends Thread{
    private Socket s;
    private BufferedReader in;
    private PrintWriter out;
    private boolean isNagle = true;

    public Client(InetAddress ia) throws IOException {
        try{
            s = new Socket(ia, 10001);
            s.setTcpNoDelay(isNagle); //是否启用Nagle算法
        } catch (IOException e) {}
        try{
            in = new BufferedReader(new InputStreamReader(s.getInputStream()));
            out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(s.getOutputStream())), true);
            start();
        }catch(IOException e1){
            s.close();
        }

    }

    public void run() {
        Scanner scan = new Scanner(System.in);
        while (true){
            String str = scan.next();
            out.println(str);
            if (str.trim().equals("end")) break;
            try {
                System.out.println(in.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        try {
            s.close();
        } catch (IOException e){
            e.printStackTrace();
        }
    }

    public static void main(String args[]) throws IOException {
        InetAddress ia = InetAddress.getByName(null); //null mean localhost
        new Client(ia);
    }
}
